from page.net_project_page import NetProjectPage
import time as t
import random as r
from selenium.common.exceptions import NoSuchElementException


class CoursePerformancePage(NetProjectPage):

    mould_list = ["FIAS弗兰德斯师生语言互动"]  # 需评价的模板

    def enter_course(self, title='听评课测试'):
        self.find_static_text(title).click()

    def star_click_single(self, star_list_page):  # 评分当前页面指标
        temp = [[x for x in star_list_page[5*x:5*x+5]] for x in range(len(star_list_page) // 5)]  # 分为5个一组
        star_group_list = list(filter(lambda x: 109 <= x[0].location['y'] <= 622, temp))  # 筛选出在当前屏幕内的星星
        for i in range(len(star_group_list)):
            r1 = r.randint(0, 4)  # 生成随机数，点击随机星级
            star_group_list[i][r1].click()

    def star_click(self):  # 评分所有指标并提交
        while True:
            t.sleep(1)
            star_list = self.find_buttons("TPK灰星")  # 获取当前页面中所有星星
            try:
                self.star_click_single(star_list)
                self.find_button('提交').click()
                # 判断是否提交成功并跳转到下个界面
                t.sleep(2)
                self.find_static_text(string='个模板进行评价', contains_flag=1)
            except NoSuchElementException:
                self.swipe_dr('up')
                continue
            else:
                break

    def performance_mould(self, mould_list):
        for each in mould_list:
            self.find_static_text(each).click()
            self.star_click()

    def performance_course(self):
        self.find_button("去评课").click()
        self.performance_mould(self.mould_list)
        self.find_static_text("请输入评价内容......").click()
        self.find_xpath("//XCUIElementTypeStaticText[@name='请输入内容']/../XCUIElementTypeTextView")\
            .send_keys("这是综合评价的内容。。。")
        self.find_button("发布").click()
        t.sleep(2)
        self.find_button("top返回").click()

    def view_report(self):
        self.find_button("分析报告").click()
        t.sleep(5)
        for i in range(3):
            t.sleep(1)
            self.swipe_dr("up")
        self.find_button("top返回").click()

    def view_course(self):
        self.find_static_text("课堂实录").click()
        self.find_button("play").click()
        t.sleep(3)
        self.find_text_field(string="点击发送评论...", type='value').click()
        t.sleep(1)
        self.find_text_field(string="点击发送评论...", type='value').send_keys("这是评论的内容")
        self.find_static_text("评论时光轴").click()  # 点击以收回键盘
        self.find_button("全部").click()
        self.find_static_text("学员").click()
        t.sleep(3)
        self.find_button("top返回").click()


if __name__ == '__main__':
    test = CoursePerformancePage()
    test.enter_project()
    test.enter_tools("听评课")
    test.enter_course("很长的视频")
    test.performance_course()
